package com.zhupanlin.zhyl.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhupanlin.zhyl.cmn.mapper.DictMapper;
import com.zhupanlin.zhyl.cmn.service.DictService;
import com.zhupanlin.zhyl.model.cmn.Dict;
import com.zhupanlin.zhyl.model.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/11/21 23:02
 */
public class DictListener extends AnalysisEventListener<DictEeVo> {

    private final DictService dictService;
    private List<Dict> dictList;

    /**
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        super.invokeHeadMap(headMap, context);
        dictList = new ArrayList<>();
    }

    public DictListener(final DictService dictService) {
        this.dictService = dictService;
    }

    /**
     * // 一行一行读取
     * @param dictEeVo
     * @param analysisContext
     */
    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        // 调用方法添加数据库
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo, dict);
        dictList.add(dict);
    }

    /**
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        dictService.saveOrUpdateBatch(dictList);
    }
}
